Page({
    data: {
        xx: {
            address: "",
            city: "",
            headimg: "",
            mobile: "",
            province: "",
            realname: "",
            sfz: "",
            laoren: []
        },
        navtop: "",
        region: [],
        region1: [],

    },
    gitfor() {
        wx.navigateBack({
            delta: -1
        })
    },

    //返回上一个页面
    gethui() {
        wx.navigateBack({
            delta: 1
        })
    },
    onLoad() {
        this.setData({
            openid: getApp().globalData.openid
        })
        // 导航栏
        // 1.获取屏幕可使用宽度
        let windowWidth = wx.getSystemInfoSync().windowWidth;
        // 2.获取状态栏高度
        const statusBarHeight = wx.getSystemInfoSync().statusBarHeight;
        //3.获取胶囊按钮位置信息
        const menuButton = wx.getMenuButtonBoundingClientRect();
        this.setData({
            navtop: menuButton.top
        })
    },
    onShow() {
        this.getdata()
    },
    getdata() {
        const app = getApp()
        const that = this
        wx.request({
            url: 'https://dj.ifankui.com/api/usrEn/getProfile',
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                openid: app.globalData.openid
            },
            success: (res) => {
                this.setData({
                    xx: res.data.data,
                    region: [res.data.data.province, res.data.data.city, res.data.data.area]
                })
                console.log(res.data.data);
                console.log(app.globalData);
                if (app.globalData.huancun.xingming.length !== 0 || app.globalData.huancun.shenfenzheng.length !== 0 || app.globalData.huancun.shoujihao.length !== 0 || app.globalData.huancun.xiangxidizhi.length !== 0) {
                    this.setData({
                        xx: {
                            realname: app.globalData.huancun.xingming,
                            mobile: app.globalData.huancun.shoujihao,
                            sfz: app.globalData.huancun.shenfenzheng,
                            address: app.globalData.huancun.xiangxidizhi,
                            laoren: res.data.data.laoren,
                            region: [app.globalData.huancun.shengshiqu[0], app.globalData.huancun.shengshiqu[1], app.globalData.huancun.shengshiqu[2]]
                        }
                    })
                    this.getadd()

                } else if (app.globalData.huancun.jianhuren.length !== 0) {
                    this.setData({
                        xx: {
                            realname: res.data.data.realname,
                            mobile: res.data.data.mobile,
                            address: res.data.data.address,
                            sfz: res.data.data.sfz,
                            laoren: app.globalData.huancun.jianhuren,
                            region: [res.data.data.province, res.data.data.city, res.data.data.area]
                        }
                    })
                } else if (res.data.data.laoren.length == 0) {
                    this.getadd()
                }else {
                    this.setData({
                        xx: res.data.data,
                        region: [res.data.data.province, res.data.data.city, res.data.data.area]
                    })
                }
                console.log(this.data.region);
            }
        })
    },

    bindRegionChange: function (e) {
        console.log(e.detail.value)
        console.log(e.detail.code)

        this.setData({
            region: e.detail.value,


        })

    },

    bindRegionChange1: function (e) {
        console.log(e.detail.value)
        const item = e.detail.value
        const index = e.currentTarget.dataset.index;

        this.setData({
            ['xx.laoren[' + index + '].province']: item[0],
            ['xx.laoren[' + index + '].city']: item[1],
            ['xx.laoren[' + index + '].area']: item[2],
            ['xx.laoren[' + index + '].id']: 0
        }, () => {
            console.log(this.data.xx.laoren);
        })


    },
    getadd() {
        console.log(this.data.xx.laoren);
        this.data.xx.laoren.push({})
        this.setData({
            'xx.laoren': this.data.xx.laoren
        })
    },
    getsubtract() {
        this.data.xx.laoren.pop()
        this.setData({
            'xx.laoren': this.data.xx.laoren
        })
    },


    formSubmit: function (e) {
        const that = this
        console.log(e);
        const userdata = e.detail.value
        const app = getApp();
        const namePattern = /^[^\d]{1,9}$/;
        let set1 = true


        if (namePattern.test(userdata.realname) == false) {
            wx.showToast({
                title: '请输入正确的姓名', // 提示的内容
                icon: 'none', // 图标，有效值：success, loading, none
                duration: 2000 // 提示的延迟时间，单位毫秒
            });
            set1 = false;
        };
        const phonePattern = /^(?!12)\d{11}$/;
        if (!phonePattern.test(userdata.mobile)) {
            wx.showToast({
                title: '请输入正确的电话号码', // 提示的内容
                icon: 'none', // 图标，有效值：success, loading, none
                duration: 2000 // 提示的延迟时间，单位毫秒
            });
            set1 = false;
        };
        const idPattern = /^\d{17}[\dXx]$/;
        if (!idPattern.test(userdata.sfz)) {
            wx.showToast({
                title: '请输入正确的身份证号码', // 提示的内容
                icon: 'none', // 图标，有效值：success, loading, none
                duration: 2000 // 提示的延迟时间，单位毫秒
            });
            set1 = false;
        };
        const regex = /[@#$%^&*()!~`\\:;'<>?,./\[\]{}|\\]/;

        console.log(userdata.address);
        if (regex.test(userdata.address)) {
            wx.showToast({
                title: '地址内包含非法字符', // 提示的内容
                icon: 'none', // 图标，有效值：success, loading, none
                duration: 2000 // 提示的延迟时间，单位毫秒
            });
            set1 = false;
        };

        if (Object.keys(this.data.xx.laoren[0]).length !== 0) {
            this.data.xx.laoren.forEach(i => {
                console.log(i);
                if (Object.keys(i).length === 0) {
                    this.getsubtract();
                    set1 = false;

                }
                if (namePattern.test(i.xm) == false) {
                    wx.showToast({
                        title: '请输入正确的老人姓名', // 提示的内容
                        icon: 'none', // 图标，有效值：success, loading, none
                        duration: 2000 // 提示的延迟时间，单位毫秒
                    });
                    set1 = false;
                }
                const phonePattern = /^(?!12)\d{11}$/;
                if (!phonePattern.test(i.dh)) {
                    wx.showToast({
                        title: '请输入正确的老人电话号码', // 提示的内容
                        icon: 'none', // 图标，有效值：success, loading, none
                        duration: 2000 // 提示的延迟时间，单位毫秒
                    });
                    set1 = false;
                }
                const idPattern = /^\d{17}[\dXx]$/;
                if (!idPattern.test(i.sfz)) {
                    wx.showToast({
                        title: '请输入正确的身份证号码', // 提示的内容
                        icon: 'none', // 图标，有效值：success, loading, none
                        duration: 2000 // 提示的延迟时间，单位毫秒
                    });
                    set1 = false;
                }

                const regex1 = /[@#$%^&*()!~`\\:;'<>?,./\[\]{}|\\]/;
                console.log(i);
                if (regex1.test(i.address)) {
                    wx.showToast({
                        title: '地址内包含非法字符1', // 提示的内容
                        icon: 'none', // 图标，有效值：success, loading, none
                        duration: 2000 // 提示的延迟时间，单位毫秒
                    });
                    set1 = false;
                };
            })
        }


        if (set1 == true) {
            wx.request({
                url: 'https://dj.ifankui.com/api/usrEn/saveProfile',
                method: "POST",
                header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                data: {
                    openid: app.globalData.openid,
                    realname: userdata.realname,
                    mobile: userdata.mobile,
                    sfz: userdata.sfz,
                    province: that.data.region[0],
                    city: that.data.region[1],
                    area: that.data.region[2],
                    address: userdata.address,
                    laoren: JSON.stringify(this.data.xx.laoren),
                },
                success(res) {
                    console.log(res);
                    if (res.data.status == "error") {


                        wx.showToast({
                            title: res.data.msg,
                            icon: 'none'
                        });
                    } else {
                        wx.showToast({
                            title: "修改成功",
                            icon: 'none'
                        });
                    }

                }
            })
        }



    },
    // 处理监护人姓名
    oldname(e) {
        console.log(e.detail.value);
        const app = getApp()
        app.globalData.huancun.xingming = e.detail.value
    },
    // 处理监护人身份证号
    oldaddress(e) {
        const app = getApp()
        app.globalData.huancun.shenfenzheng = e.detail.value
    },
    // 处理监护人手机号
    oldphone(e) {
        const app = getApp()
        app.globalData.huancun.shoujihao = e.detail.value
    },
    // 处理监护人详细地址
    oldcity(e) {
        const app = getApp()
        app.globalData.huancun.xiangxidizhi = e.detail.value
    },
    xxx(e) {
        const that = this
        const item = e.detail.value
        const index = e.currentTarget.dataset.index;
        console.log(index, item);
        this.setData({
            ['xx.laoren[' + index + '].xm']: item
        }, () => {
            const app = getApp()
            app.globalData.huancun.jianhuren = this.data.xx.laoren
        })
        const app = getApp()
        console.log(this.data.xx.laoren);
    },
    xxx1(e) {
        const that = this
        const item = e.detail.value
        const index = e.currentTarget.dataset.index;
        console.log(index, item);
        this.setData({
            ['xx.laoren[' + index + '].dh']: item
        }, () => {
            const app = getApp()
            app.globalData.huancun.jianhuren = this.data.xx.laoren
        })
        console.log(this.data.xx.laoren);
    },
    xxx2(e) {
        const that = this
        const item = e.detail.value
        const index = e.currentTarget.dataset.index;
        console.log(index, item);
        this.setData({
            ['xx.laoren[' + index + '].sfz']: item
        }, () => {
            const app = getApp()
            app.globalData.huancun.jianhuren = this.data.xx.laoren
        })
        console.log(this.data.xx.laoren);
    },

    xxx3(e) {
        const that = this
        const item = e.detail.value
        const index = e.currentTarget.dataset.index;
        console.log(index, item);
        this.setData({
            ['xx.laoren[' + index + '].address']: item
        }, () => {
            const app = getApp()
            app.globalData.huancun.jianhuren = this.data.xx.laoren
        })
        console.log(this.data.xx.laoren);
    },

    shanchu(e) {
        const that = this
        const index = e.currentTarget.dataset.index;
        const lrid = e.currentTarget.dataset.lrid;
        const app = getApp();
        console.log(e);
        console.log(lrid);
        wx.request({
            url: 'https://dj.ifankui.com/api/usrEn/deleteLoaren',
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                openid: app.globalData.openid,
                laorenId: lrid
            },
            success(res) {
                console.log(res);
                let shanchu = that.data.xx.laoren
                shanchu.splice(index, 1);
                that.setData({
                    laoren: shanchu
                })
                that.onShow()
            }
        })
    }




})